package cn.flying.cloud.base.core.configuration;

import javax.annotation.Resource;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.flying.cloud.base.core.interceptor.AbstractRequestInterceptor;
import cn.flying.cloud.base.core.interceptor.RequestContextInterceptor;
import cn.flying.cloud.base.core.web.method.CustomHandlerMethodArgumentResolver;

/**
 * web应用配置
 *
 * @author: admin
 * @date: 2023年02月20日 18:32
 * @version: 1.0
 */
@Configuration
public class WebAppAutoConfiguration implements WebMvcConfigurer {

    @Resource
    private ApplicationContext applicationContext;

    /**
     * Add Spring MVC lifecycle interceptors for pre- and post-processing of
     * controller method invocations and resource handler requests.
     * Interceptors can be registered to apply to all requests or be limited
     * to a subset of URL patterns.
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自己的拦截器,并设置拦截的请求路径
        //addPathPatterns为拦截此请求路径的请求
        //excludePathPatterns为不拦截此路径的请求
        String[] exPath = {"/static/**", "/css/**", "/font/**", "/images/**", "/js/**", "/layui/**", "/lib/**", "/style/**", "/ztree/**", "/modules/**", "/adminui/**", "/config.js", "/oauth/static/**", "/font-awesome-4.7.0/**",
                "/oauth/login/**", "/oauth/logout", "/oauth/register/**", "/oauth/forget/form", "/oauth/captcha", "/oauth/send/code", "/rsa/publicKey", "/oauth/reset",
                "/auth/captcha", "/auth/login/**", "/auth/logout", "/auth/forget", "/auth/register/**", "/auth/code", "/auth/valid/code", "/resetpass"};
//        String[] exPath = {"/**"};

        registry.addInterceptor(new RequestContextInterceptor()).addPathPatterns("/**");

        Map<String, AbstractRequestInterceptor> beans = applicationContext.getBeansOfType(AbstractRequestInterceptor.class);
        List<AbstractRequestInterceptor> list = beans.values().stream().sorted(Comparator.comparingInt(AbstractRequestInterceptor::getOrder)).collect(Collectors.toList());
        list.forEach(interceptor -> registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns(exPath));
    }


    /**
     * 配置参数映射
     *
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new CustomHandlerMethodArgumentResolver());
    }

//    /**
//     * 跨域请求配置
//     *  注意：如果有gateway网关已经统一做了跨域配置，这里就不需要了，因为重复的配置会导致的失效，不会回传给客户端
//     *
//     * @param registry
//     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                // 允许所有源访问，可以指定具体的源
//                .allowedOrigins("*")
//                // 允许的请求方法
//                .allowedMethods("*")
//                // 允许所有请求头
//                .allowedHeaders("*")
//                // 是否允许发送Cookie
//                .allowCredentials(true)
//                // 预检请求的有效期，单位为秒
//                .maxAge(3600);
//    }
}
